package com.example.uniplugin_events;

import android.app.Activity;
import android.os.SystemClock;
import android.view.MotionEvent;

import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.common.WXModule;

public class VirtualEvents extends WXModule {
    @JSMethod(uiThread = true)
    public JSONObject TouchEvents(int x, int y){
        JSONObject data = new JSONObject();
        try{
            final long downTime = SystemClock.uptimeMillis();
            final MotionEvent downEvent = MotionEvent.obtain(
                    downTime,downTime,MotionEvent.ACTION_DOWN,x,y,0);
            final MotionEvent upEvent = MotionEvent.obtain(
                    downTime,SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,x,y,0
            );

            Activity mActivity  = (Activity) mWXSDKInstance.getContext();
            mActivity.getWindow().superDispatchTouchEvent(downEvent);
            mActivity.getWindow().superDispatchTouchEvent(upEvent);
            data.put("code","success");
            data.put("location.x",x);
            data.put("location.y",y);

            return data;
        }catch (Exception e){
            data.clear();
            data.put("Error","错误");
            data.put("Error",e.getMessage());
            return data;
        }
    }
}
